package com.Service;

import com.Dao.AdminDao;
import com.Dao.EngineerDao;
import com.Dao.UserDao;
import com.Dao.WeiduDao;
import com.POJO.Admin;
import com.POJO.Engineer;
import com.POJO.User;
import com.POJO.Weidu;
import com.Service.Imp.WeiduServiceImp;
import com.Utils.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WeiduService extends ServiceImpl<WeiduDao, Weidu> implements WeiduServiceImp {

    @Autowired
    private WeiduDao weiduDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private EngineerDao engineerDao;
    @Autowired
    private AdminDao adminDao;



//未读消息添加
public int add(String sendertel,String recipienttel,String content){
    //   01      -->>  02
    //  sendertel    recipienttel
    QueryWrapper<Weidu> wrapper=new QueryWrapper<>();
    wrapper.eq("sendertel",sendertel);
    wrapper.eq("recipienttel",recipienttel);
    Weidu weidu;
    Weidu weidu2;
    String name=findName(sendertel);
    String name2=findName(recipienttel);
    try
    {   weidu=weiduDao.selectOne(wrapper);
        weidu.setUsername(name);
        weidu.setNum(weidu.getNum()+1);
        weidu.setContent(content);
        return  weiduDao.update(weidu,wrapper);
    } catch (Exception e)
    {   weidu=new Weidu();
        weidu.setUsername(name);
        weidu.setSendertel(sendertel);
        weidu.setRecipienttel(recipienttel);
        weidu.setContent(content);
        weidu.setNum(1);
        weidu2=new Weidu();
        weidu2.setUsername(name2);
        weidu2.setSendertel(recipienttel);
        weidu2.setRecipienttel(sendertel);
        weidu2.setContent(content);
        weidu2.setNum(0);
        weiduDao.insert(weidu2);
        return weiduDao.insert(weidu);
    }
}

    //未读消息添加
    public int weiduListAdd(String sendertel,String recipienttel,String content){
      Weidu weidu;
      weidu=new Weidu();

      QueryWrapper<Weidu> wrapper=new QueryWrapper<>();
      wrapper.eq("sendertel",sendertel);
      wrapper.eq("recipienttel",recipienttel);
      if(weiduDao.selectOne(wrapper)==null){
          String name=findName(sendertel);
          String name2=findName(recipienttel);
          weidu.setUsername(name);
          weidu.setSendertel(sendertel);
          weidu.setRecipienttel(recipienttel);
          weidu.setContent(content);
          weidu.setNum(0);
          Weidu weidu2=new Weidu();
          weidu2.setUsername(name2);
          weidu2.setSendertel(recipienttel);
          weidu2.setRecipienttel(sendertel);
          weidu2.setContent(content);
          weidu2.setNum(0);
          weiduDao.insert(weidu2);
          return weiduDao.insert(weidu);
      }
      else{
          return 1;
      }
    }
    //用户查询未读消息
public List<Weidu> select(String sendertel){
    QueryWrapper<Weidu> wrapper=new QueryWrapper<>();
    wrapper.eq("recipienttel",sendertel);
    return weiduDao.selectList(wrapper);
}

    //单击过后消除未读消息
public int del(String sendertel, String recipienttel){
//    weiduDao=BeanUtils.getBean(WeiduDao.class);
    QueryWrapper<Weidu> wrapper=new QueryWrapper<>();
    wrapper.eq("sendertel",sendertel);
    wrapper.eq("recipienttel",recipienttel);

    Weidu weidu=weiduDao.selectOne(wrapper);
    if(weidu!=null) {
        weidu.setNum(0);
        return weiduDao.update(weidu,wrapper);
    } else
         return 1;
}

public  String findName(String tel){
    String name="";
    QueryWrapper<User> s=new QueryWrapper<>();
    s.eq("tel",tel);
    QueryWrapper<Engineer> t=new QueryWrapper<>();
    t.eq("tel",tel);
    QueryWrapper<Admin> a=new QueryWrapper<>();
    a.eq("admin",tel);
    if(engineerDao.selectOne(t)!=null){
        name=engineerDao.selectOne(t).getUsername();
        System.out.println(name);
        System.out.println("11111111111111111111");
    }else if(userDao.selectOne(s)!=null) {
        name = userDao.selectOne(s).getUsername();
        System.out.println(name);
        System.out.println("22222222222222222222");
    }else if(adminDao.selectOne(a)!=null){
        name = adminDao.selectOne(a).getName();
        System.out.println(name);
        System.out.println("333333333333333333333333");
    }
    return name;
}

}
